package com.gxa.luckin.shop.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.luckin.core.exception.BusinessException;
import com.gxa.luckin.core.exception.MessageCode;
import com.gxa.luckin.core.pojo.Shop;
import com.gxa.luckin.core.utils.JsonResult;
import com.gxa.luckin.shop.service.ShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Min;
import java.util.List;

/**
 * @author: fanyouzhao
 * @date: 14:10 2021/7/16
 */
@RestController
@RequestMapping("/shop")
@Api(tags = "门店模块")
@CrossOrigin
@Validated
public class ShopController {

    @Resource
    ShopService shopService;

    @PostMapping("/shopById")
    @ApiOperation(value = "shopById通过Id查询门店信息")
    public JsonResult shopById(@RequestParam("id") Integer id) throws BusinessException {
        Shop shop = shopService.selectById(id);
        if(shop==null){
            throw new BusinessException(MessageCode.SHOP_IS_EMPTY);
        }
        return new JsonResult("200","success",shop);
    }

    @PostMapping("/shopByPage")
    @ApiOperation(value = "shopByPage分页查询")
    public JsonResult shopByPage(
            @Min(value = 1,message = "页码最小不能小于1") @RequestParam("page")Integer page,
            @Min(value = 1,message = "每页大小最小不能小于1") @RequestParam("size")Integer size){
        PageInfo<Shop> shopPageInfo = shopService.selectByPage(page, size);
        return new JsonResult("200","success",shopPageInfo);
    }

    @PostMapping("/shopLikeByPage")
    @ApiOperation(value = "shopLikeByPage分页查询(可带多条件模糊查询)")
    public JsonResult shopLikeByPage(
            @Min(value = 1,message = "页码最小不能小于1") @RequestParam("page")Integer page,
            @Min(value = 1,message = "每页大小最小不能小于1") @RequestParam("size")Integer size,
            @RequestBody(required = false) Shop shop) throws IllegalAccessException {
        if (shop==null){
            PageInfo<Shop> shopPageInfo = shopService.selectByPage(page, size);
            return new JsonResult("200","success",shopPageInfo);
        }else{
            PageHelper.startPage(page,size);
            List<Shop> shops = shopService.selectByLike(shop);
            return new JsonResult("200","success",PageInfo.of(shops));
        }
    }

    @PostMapping("/addShop")
    @ApiOperation(value = "addShop添加门店")
    public JsonResult addShop(@RequestBody Shop shop) throws BusinessException {
        Integer add = shopService.add(shop);
        if (add<0){
            throw new BusinessException(MessageCode.SHOP_ADD_ERROR);
        }
        Shop shopBack = shopService.selectById(shop.getId());
        return new JsonResult("200","success",shopBack);
    }

    @PostMapping("/updateShop")
    @ApiOperation(value = "updateShop根据Id修改门店信息")
    public JsonResult updateShop(@RequestBody Shop shop) throws BusinessException {
        Integer update = shopService.updateById(shop);
        if (update>0){
            Shop shopBack = shopService.selectById(shop.getId());
            return new JsonResult("200","success",shopBack);
        }else {
            throw new BusinessException(MessageCode.SHOP_UPDATE_ERROR);
        }
    }

    @PostMapping("/delShop")
    @ApiOperation(value = "delShop通过Id删除门店")
    public JsonResult delShop(@RequestParam("id")Integer id) throws BusinessException {
        Integer del = shopService.deleteById(id);
        if (del<0){
            throw new BusinessException(MessageCode.SHOP_DEL_ERROR);
        }
        return new JsonResult("200","success");
    }

}